package com.gxgeek.userserver.controller;

import com.gxgeek.userserver.constant.CookieConstant;
import com.gxgeek.userserver.constant.RedisConstant;
import com.gxgeek.userserver.dataobject.UserInfo;
import com.gxgeek.userserver.enums.ResultEnum;
import com.gxgeek.userserver.enums.RoleEnum;
import com.gxgeek.userserver.service.UserService;
import com.gxgeek.userserver.util.CookieUtil;
import com.gxgeek.userserver.vo.ResultVO;
import com.gxgeek.userserver.vo.ResultVOUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author gaoxing
 * @create 2018-04-02 下午2:37
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserService userService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openId") String openId,
                          HttpServletResponse response) {
        //查出用户
        UserInfo userInfo = userService.findByOpenId(openId);
        //判断角色
        if (userInfo == null){
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }else if (RoleEnum.BUTER.getCode().intValue() != (userInfo.getRole())){
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);

        }
        //设置cookie
        CookieUtil.set(response, CookieConstant.OPENID,openId,CookieConstant.EXPIRE);
        return ResultVOUtil.success();
    }


    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openId") String openId,
                          HttpServletRequest request,
                          HttpServletResponse response) {
        String cookieValue = CookieUtil.get(request, CookieConstant.TOKEN);
        if (StringUtils.isNotBlank(cookieValue)&&
                StringUtils.isNotBlank(redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN,cookieValue)))){
            return ResultVOUtil.success();
        }
        //查出用户
        UserInfo userInfo = userService.findByOpenId(openId);
        //判断角色
        if (userInfo == null){
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }else if (RoleEnum.SELLER.getCode().intValue() != (userInfo.getRole())){
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);

        }
        //
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN,token),openId,
                expire, TimeUnit.SECONDS);
        //设置cookie
        CookieUtil.set(response, CookieConstant.TOKEN,token,CookieConstant.EXPIRE);
        return ResultVOUtil.success();
    }

}
